package com.snoob.bffApi.wConfig.response;

import com.snoob.springcloudalibaba.base.BaseResult;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @描述 统一封装响应数据
 * @作者 lixing_java@163.com
 * @日期 2023/2/6
 */
@RestControllerAdvice
public class ControllerResponseAdvice implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        // response是BaseResult类型，或者注释了NotControllerResponseAdvice都不进行包装
        return !(
                returnType.hasMethodAnnotation(NotControllerResponseAdvice.class)
                || returnType.getParameterType().isAssignableFrom(BaseResult.class)
        );
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        return new BaseResult<Object>().success(body);
    }
}
